package com.bob.light

import android.app.Application
import com.bob.light.config.AppConfig
import com.tuya.smart.android.common.utils.L
import java.util.concurrent.ExecutorService
import java.util.concurrent.LinkedBlockingQueue
import java.util.concurrent.ThreadPoolExecutor
import java.util.concurrent.TimeUnit

class App : Application() {

    override fun onCreate() {
        super.onCreate()
        initConfig()
        registerUncaughtExceptionHandler()
        initTuyaSDK()
    }

    private fun initConfig() {
        AppConfig.newBuilder()
                .executorService(initExecutorService())
                .application(this@App)
                .debug(BuildConfig.DEBUG)
                .build()
    }

    private fun initTuyaSDK() {
        TuyaHomeSDKHelper.init()
    }

    private fun initExecutorService(): ExecutorService {
        return ThreadPoolExecutor(1, Int.MAX_VALUE, 20,
            TimeUnit.SECONDS, LinkedBlockingQueue())
    }

    private fun registerUncaughtExceptionHandler() {
        if (AppConfig.isDebug()) {
            Thread.setDefaultUncaughtExceptionHandler { t, e ->
                val tag = getString(R.string.app_name)
                L.d(tag, "t = ${t.name}, msg = ${e.message}")
            }
        }
    }
}